package ink.ykb.redisLockDemo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import ink.ykb.redisLockDemo.config.CodeConstant;
import ink.ykb.redisLockDemo.config.MessageUtil;
import ink.ykb.redisLockDemo.config.ResponseMsg;
import ink.ykb.redisLockDemo.config.lock.redis.RedisLock;
import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping("/")
@Slf4j
public class TestController {
	
	@Autowired
	private RestTemplate restTemplate;
	@Autowired
	private MessageUtil messageUtil;
	
	private int num1 = 0;
	private int num2 = 0;
	private int num3 = 0;
	
	@RedisLock(prefix= "lock:test1:",key = "t",isBolck=true)
	@RequestMapping("/test1")
	public ResponseMsg<Integer> test1(String t) {
		num1++;
		log.info("num1：{}",num1);
		return new ResponseMsg<>(CodeConstant.SUCCESS_000000,messageUtil.getMessage(CodeConstant.SUCCESS_000000),num1);
	}
	
	@RedisLock(prefix= "lock:test2:",key = "t",isBolck=true)
	@RequestMapping("/test2")
	public ResponseMsg<Integer> test2(String t) {
		num2++;
		log.info("num2：{}",num2);
		return new ResponseMsg<>(CodeConstant.SUCCESS_000000,messageUtil.getMessage(CodeConstant.SUCCESS_000000),num2);
	}
	
	@RedisLock(prefix= "lock:test3:",key = "t",isBolck=true)
	@RequestMapping("/test3")
	public ResponseMsg<Integer> test3(String t) {
		num3++;
		log.info("num3：{}",num3);
		return new ResponseMsg<>(CodeConstant.SUCCESS_000000,messageUtil.getMessage(CodeConstant.SUCCESS_000000),num3);
	}

	@RequestMapping("/test")
	public ResponseMsg<String> test(int xh) {
		num1 = 0;
		num2 = 0;
		num3 = 0;
		for (int i = 0; i < xh; i++) {
			new Thread(() ->{
				String result = restTemplate.getForObject("http://127.0.0.1:8080/test1?t=4", String.class);
				
//				int a  = (int)(Math.random() * 3);
////				int a = 0;
//				String url = "";
//				if(a == 0) {
//					url = "http://127.0.0.1:8080/test1?t=1";
//				}else if(a == 1) {
//					url = "http://127.0.0.1:8080/test2?t=2";
//				}else if(a == 2) {
//					url = "http://127.0.0.1:8080/test3?t=3";
//				}
//				String result = restTemplate.getForObject(url, String.class);
				
				
//				String result = restTemplate.getForObject("http://127.0.0.1:8080/test4?str=4", String.class);
				
//				log.info("\nresult：{}",result);
			}).start();
		}
		try {
			Thread.sleep(3*1000);
		} catch (InterruptedException e) {
		}
		
		return new ResponseMsg<>(CodeConstant.SUCCESS_000000,messageUtil.getMessage(CodeConstant.SUCCESS_000000),null);
	}
	
	@RedisLock(prefix= "lock:test4:",key = "str",code = CodeConstant.test_000002)
	@RequestMapping("/test4")
	public ResponseMsg<String> test4(String str) {
		return new ResponseMsg<>(CodeConstant.SUCCESS_000000,messageUtil.getMessage(CodeConstant.SUCCESS_000000),str);
	}
	
}
